package org.jim.server.command.handler;

import com.huiyun.project.im.consts.ImCsSessionConst;
import lombok.extern.slf4j.Slf4j;
import org.jim.core.ImChannelContext;
import org.jim.core.ImConst;
import org.jim.core.ImPacket;
import org.jim.core.ImStatus;
import org.jim.core.exception.ImException;
import org.jim.core.helper.cs.ICsSessionHelper;
import org.jim.core.packets.pub.consts.Command;
import org.jim.core.packets.RespBody;
import org.jim.core.packets.cs.session.command.CsSessionReqCmd;
import org.jim.core.packets.cs.session.command.CsSessionRespCmd;
import org.jim.core.packets.cs.session.vo.JimCsChangeSessionResp;
import org.jim.core.packets.cs.session.vo.JimCsCreateSessionResp;
import org.jim.core.utils.JsonKit;
import org.jim.server.JimServerAPI;
import org.jim.server.command.AbstractCmdHandler;
import org.jim.server.command.CommandHandler;
import org.jim.server.processor.session.CsSessionCmdProcessor;
import org.jim.server.protocol.ProtocolManager;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 版本: [1.0]
 * 功能说明 会话cmd消息命令处理器,创建或者检查session
 */
@CommandHandler(cmd = Command.COMMAND_SESSION_REQ, cmdProcessorClasses = CsSessionCmdProcessor.class)
@Slf4j
public class CsSessionReqHandler extends AbstractCmdHandler {
    @Autowired
    private ICsSessionHelper csSessionHelper;

    @Override
    public ImPacket handler(ImPacket packet, ImChannelContext channelContext) throws ImException {
        if (packet.getBody() == null) {
            return ProtocolManager.Converter.respPacket(new RespBody(Command.COMMAND_SESSION_RESP, ImStatus.C10032), channelContext);
        }
        //请求中构建出请求对象
        CsSessionReqCmd sessionReqBody = JsonKit.toBean(packet.getBody(), CsSessionReqCmd.class);
        //当前登录用户
        String userId = channelContext.getUserId();//登录用户名

        //执行查询
        CsSessionCmdProcessor sessionCmdProcessor = this.getSingleProcessor(CsSessionCmdProcessor.class);
        CsSessionRespCmd respBody = sessionCmdProcessor.createCsSession(userId, sessionReqBody);
        if (null != respBody.getData()) {
            //session创建成功
            JimCsCreateSessionResp jimCsCreateSessionResp = (JimCsCreateSessionResp) respBody.getData();
            //temp 开头 临时会话  客服不在线
            if(!jimCsCreateSessionResp.getSessionId().startsWith(ImConst.TEMP)){
                //给客服发送新消息接入 开始
                //1,查询会话详情
//                JimCsSession sessionResp = this.csSessionHelper.get(jimCsCreateSessionResp.getSessionId());
                //2，设置返回数据
                JimCsChangeSessionResp changeSessionResp=new JimCsChangeSessionResp();
                changeSessionResp.setChangeType(ImCsSessionConst.SESSION_CHANGE_TYPE.NEW.getValue());
                changeSessionResp.setData(jimCsCreateSessionResp);
                ImPacket chatPacket = new ImPacket(Command.COMMAND_CHANGE_SESSION_RESP, new RespBody(Command.COMMAND_CHANGE_SESSION_RESP, changeSessionResp).toByte());
                chatPacket.setSynSeq(packet.getSynSeq());
                //发送到对应的客服
                JimServerAPI.sendToUser(jimCsCreateSessionResp.getCsId(), chatPacket);
                //给客服发送接入消息  结束
            }

        }
        //发送回调给 客户
        return ProtocolManager.Converter.respPacket(respBody, channelContext);
    }

    @Override
    public Command command() {
        return Command.COMMAND_SESSION_REQ;
    }
}
